package sort.impl;

import anaydis.sort.SorterType;
import com.sun.istack.internal.NotNull;

import java.util.Comparator;
import java.util.List;

public class HSorter extends AbstractSorter {
    int h;

    public HSorter() {
        super(SorterType.H);
        h = 11;
    }

    public HSorter(SorterType shell) {
        super(shell);
        h = 11;
    }

    public void setH(int h) {
        this.h = h;
    }

    public <T> void sort(@NotNull Comparator<T> tComparator, @NotNull List<T> ts) {
        hSort(tComparator, ts);
    }

    protected <T> void hSort(Comparator<T> tComparator, List<T> list) {
        if (h < list.size()) {
            for (int i = h; i < list.size(); i++) {
                for (int j = i; j >= h && !greater(tComparator, list, j, j - h); j -= h) {
                    swap(list, j, j - h);
                }
            }
        }
    }
}
